//
//  ProvinceAndCityTableView.m
//  QuickCure1
//
//  Created by Oopsjesse on 05/11/2016.
//  Copyright © 2016 maoge. All rights reserved.
//

#import "ProvinceAndCityTableView.h"
#import "WGCityModel.h"
#import <YYModel.h>

@interface ProvinceAndCityTableView ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)NSArray<WGCityModel *> *dataList;




@end

static NSString *cellID = @"cellID";

@implementation ProvinceAndCityTableView

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadData];
    
    [self setupUI];
    
    
    
}
- (void)setupUI{


    UITableView *tableView = [[UITableView alloc] init];
    
    [self.view addSubview:tableView];
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.view);
    }];
    
    tableView.dataSource = self;
    tableView.delegate = self;
    
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellID];
    
    
}



- (void)loadData{
    
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"province.plist" ofType:nil];
    NSArray *array = [NSArray arrayWithContentsOfFile:filePath];
    
    self.dataList = [NSArray yy_modelArrayWithClass:[WGCityModel class] json:array];
    
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return _dataList.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return _dataList[section].cities.count;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

    return _dataList[section].name;
}

-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{

    NSMutableArray *array = [NSMutableArray array];
    
    [_dataList enumerateObjectsUsingBlock:^(WGCityModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        [array addObject:obj.name];
    }];
    
    return array.copy;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    
    cell.textLabel.text = _dataList[indexPath.section].cities[indexPath.row];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{


    _callBackBlock(_dataList[indexPath.section].name,_dataList[indexPath.section].cities[indexPath.row]);
    
    [self.navigationController popViewControllerAnimated:YES];
}

@end
